<?php


namespace app\lmsx\admin;

use app\common\builder\ZBuilder;
use app\lmsx\model\Article as ArticleModel;
use app\lmsx\model\Category as CategoryModel;
use think\Db;

/**
 * 定向提分
 * @author lan
 */
class Direction extends Base
{
    public function index(){
        // 获取查询和排序规则
        $map = $this->getMap();
        $order = $this->getOrder();
        $map['ident']='direction';
        $data = Db::table('dp_lmsx_article')
       // ->view('dp_lmsx_category','name','dp_lmsx_category.id=dp_lmsx_article.type_id')
        ->where($map)
        ->order($order)
        ->order('id desc')
        ->paginate()->each(function($item){
            // 对分页数据可以做额外处理
//            $row=Db::table('dp_lmsx_category')->where('id',$item['type_id'])->field('name')->find();
//            $item['type_id']=$row['name'];
            $item['content']=msubstr($item['content'],20);
            $arr=['0'=>'暂无','1'=>'衡阳','2'=>'株洲','3'=>'岳阳'];
            $item['city']=$arr[$item['city']];
            //dump($item);die;
            return $item;
        });
    return ZBuilder::make('table')
        ->setTableName('lmsx_article')  //需要修改 这里修改正确可以直接修改字段 删除
        ->addColumns([
            ['id','id'],
            ['title','资讯标题'],
            ['cover','资讯封面图片','picture'],
            //['name','资讯分类'],
            ['city','城市'],
            //['view','浏览量'],
            //['content','资讯内容'],
            ['create_time','创建时间','datetime'],
            ['right_button', '操作', 'btn']
        ])
        ->addTopButton('add', [],[])
        ->addRightButton('detail', ['icon' => 'fa fa-fw fa-eye', 'title' => '查看详情', 'href' => url('detail', ['id' => '__id__'])])
        ->addRightButton('edit',[],[])
        ->addTopButtons(['delete'])
        ->setRowList($data)
        ->addFilter(['name'=>'lmsx_category'])
        ->addFilter('city',['1'=>'衡阳','2'=>'株洲','3'=>'岳阳'])
        ->setSearch(['id' => 'ID', 'title' => '标题','city'=>'城市'])
        ->fetch();
}
    public function add(){
        if($this->request->isPost()){
            // 第一步 获取数据 如果后台面向大众 需要用only方法
            $data = request()->post();
            // 第二部 验证数据
            $result = $this->validate($data,'Direction');
            if($result!==true) $this->error($result);
            // 第三部 存储
            ArticleModel::create($data);
            $this->success('保存成功', null, '_parent_reload');
        }
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden','ident','direction'],
                ['text', 'title', '资讯标题','必须'],
                ['image', 'cover', '资讯封面图片','必须,图片大小限2M'],
               // ['select', 'type_id', '分类','必须',CategoryModel::getDownChild(0,'direction')],
                ['radio', 'city', '城市','必须',['1'=>'衡阳','2'=>'株洲','3'=>'岳阳'],1],
                ['ueditor', 'content', '资讯内容','必须'],
                //['number', 'view', '浏览量']
            ])
            ->fetch();
    }
    public function edit($id=null){
        if($this->request->isPost()){
            $data = request()->post();
            $result = $this->validate($data,'Direction');
            if($result!==true) $this->error($result);
            ArticleModel::update($data);
            $this->success('保存成功', null, '_parent_reload');
        }
       // $data = ArticleModel::get($id);
        $data=Db::table('dp_lmsx_article')->where('id',$id)->find();

        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden','id'],
                ['text', 'title', '资讯标题','必须'],
                ['image', 'cover', '资讯封面图片','必须,图片大小限2M'],
               // ['select', 'type_id', '资讯分类','必须',CategoryModel::getDownChild(0,'direction')],
                ['radio', 'city', '城市','必须',['1'=>'衡阳','2'=>'株洲','3'=>'岳阳']],
                ['ueditor', 'content', '资讯内容','必须'],
                //['number', 'view', '浏览量']
            ])
            ->setFormData($data)
            ->fetch();
    }

    /**
     * 查看详情
     */
    public function detail($id=null){
        if (empty($id)) $this->error('参数错误');
        $info=Db::table('dp_lmsx_article')->where('id',$id)->find();
        // 使用ZBuilder快速创建表单
        return ZBuilder::make('form')
            ->setPageTitle('编辑') // 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['hidden', 'id'],
                ['static', 'title', '资讯标题'],
                //['radio', 'type_id', '资讯分类','',['中考资讯','高考资讯']],
                ['ueditor', 'content', '资讯内容'],
               // ['static', 'view', '浏览量']
            ])
            ->hideBtn('submit')
            ->setFormData($info) // 设置表单数据
            ->fetch();
    }
}